package com.sanjiahantang.youjiaclient.module.store.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.recyclerview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.uilibrary.views.ObservableScrollView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiaclient.module.decorate.views.impl.DecorateFormActivity;
import com.sanjiahantang.youjiaclient.module.evaluate.views.impl.EvaluationtStoreActivity;
import com.sanjiahantang.youjiaclient.module.store.beans.StoreDetailBean;
import com.sanjiahantang.youjiaclient.module.store.preseneters.StoreDetailPresenter;
import com.sanjiahantang.youjiaclient.module.store.views.StoreDetailView;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

/**
 * Created by yan on 2018/1/3.
 * 体验店详情界面
 */

public class StoreDetailActivity extends BaseMvpActivity<StoreDetailView, StoreDetailPresenter> implements StoreDetailView, View.OnClickListener,
        ObservableScrollView.OnScrollChangedListener, StoreDetailView.OnListLoadListener, BaseView.OnLoadListener {

    private View titelBar, mapView;
    private ObservableScrollView observableScrollView;

    private ImageView showImageView, detailBackImageView, iphoneImageView;

    private TextView storeNameTextView, addressTextView, memberNumTextView, completeProjectTextView,
            designerMoreTextView, evaluationMoreTextVew, bottomTextView;
    private LinearLayout materailsIconsLinearLayout;
    //设计师画廊
    private MagicIndicator designerGridView;
    //评价列表
    private MyListView evaluationRecyclerView;
    //体验店ID，上个界面传递过来的
    private String roleId;
    //整个数据包
    private StoreDetailBean.Data data;

    @Override
    public StoreDetailPresenter initPresenter() {
        return new StoreDetailPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_store_detail);
        titelBar = this.findViewById(R.id.activity_store_detail_titlebar);
        observableScrollView = (ObservableScrollView) this.findViewById(R.id.activity_store_detail_scroller);
        showImageView = (ImageView) this.findViewById(R.id.activity_store_detail_largeIcon);
        detailBackImageView = (ImageView) this.findViewById(R.id.activity_store_detail_back);
        storeNameTextView = (TextView) this.findViewById(R.id.activity_store_detail_storename_tv);
        materailsIconsLinearLayout = (LinearLayout) this.findViewById(R.id.activity_store_detail_materials_icons);
        mapView = this.findViewById(R.id.activity_store_detail_map_ll);
        addressTextView = (TextView) this.findViewById(R.id.activity_store_detail_address_tv);
        iphoneImageView = (ImageView) this.findViewById(R.id.activity_store_detail_iphone_iv);
        memberNumTextView = (TextView) this.findViewById(R.id.activity_store_detail_membernum_tv);
        completeProjectTextView = (TextView) this.findViewById(R.id.activity_store_detail_complete_projectnum_tv);
        designerMoreTextView = (TextView) this.findViewById(R.id.activity_store_detail_designermore_tv);
        designerGridView = (MagicIndicator) this.findViewById(R.id.activity_store_detail_designer_mi);
        evaluationMoreTextVew = (TextView) this.findViewById(R.id.activity_store_detail_evaluationmore_tv);
        evaluationRecyclerView = (MyListView) this.findViewById(R.id.activity_store_detail_evaluation_rc);
        bottomTextView = (TextView) this.findViewById(R.id.activity_store_detail_bottom_bton);
        View titleBack = this.findViewById(R.id.fl_Left);
        ((TextView) this.findViewById(R.id.title)).setText("体验店详情");


//        添加监听事件
        detailBackImageView.setOnClickListener(this);
        titleBack.setOnClickListener(this);
        mapView.setOnClickListener(this);
        iphoneImageView.setOnClickListener(this);
        designerMoreTextView.setOnClickListener(this);
        evaluationMoreTextVew.setOnClickListener(this);
        bottomTextView.setOnClickListener(this);

        observableScrollView.setOnScrollChangedListener(this);

        presenter.initContainers(this, this);

        roleId = this.getIntent().getStringExtra(KEY_ROLEID);
    }

    @Override
    public void initData() {
        presenter.loadData(this, roleId, this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            //详情页返回按键
            case R.id.activity_store_detail_back:
                finish();
                break;
            //标题栏中返回按键
            case R.id.fl_Left:
                finish();
                break;
            //地图图标点击事件
            case R.id.activity_store_detail_map_ll:
                startActivity(new Intent(this, StoreOnMapActivity.class)
                        .putExtra(KEY_LAT, data.getLat())
                        .putExtra(KEY_LON, data.getLon())
                        .putExtra(KEY_TITLE, data.getExperienceName()));
                break;
            //电话图标点击事件
            case R.id.activity_store_detail_iphone_iv:
                //用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getRoleId()));
                startActivity(intent);
                break;
            //设计师区域更多点击事件
            case R.id.activity_store_detail_designermore_tv:
                startActivity(new Intent(this, DesignerListActivity.class).putExtra(KEY_ROLEID, data.getRoleId()));
                break;
            //评价区域更多点击事件
            case R.id.activity_store_detail_evaluationmore_tv:
                startActivity(new Intent(this, EvaluationtStoreActivity.class).putExtra(KEY_ROLEID, data.getRoleId()));
                break;
            //底部我要装修点击事件
            case R.id.activity_store_detail_bottom_bton:
                if (!TextUtils.isEmpty(ConfigValue.DATA_KEY)) {
                    startActivity(new Intent(this, DecorateFormActivity.class).putExtra(KEY_EXPERIENCENAME, data.getExperienceName())
                            .putExtra(KEY_EXPERIENCEPHONE, data.getRoleId()).putExtra(KEY_FROM, "store"));
                } else {
                    LoginActivity.logout($this);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
        if (Utils.checkIsVisible(this, detailBackImageView)) {
            titelBar.setVisibility(View.GONE);
        } else {
            titelBar.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onLoad(Object content) {
        data = (StoreDetailBean.Data) content;
        Picasso.with(this).load(FileUtils.rePath(data.getUrl())).resize(250, 250).memoryPolicy(NO_CACHE, NO_STORE).placeholder(R.drawable.i_match).into(showImageView);
        storeNameTextView.setText(data.getExperienceName());
        addressTextView.setText(data.getAddress());
        memberNumTextView.setText(data.getTeamNumber() + "");
        completeProjectTextView.setText(data.getProjectNumber() + "");
        presenter.addIconsView(this, materailsIconsLinearLayout, data.getIconList());

    }

    @Override
    public void initContainers(CommonNavigator navigator, com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter evaluationAdapter) {
        designerGridView.setNavigator(navigator);
        evaluationRecyclerView.setAdapter(evaluationAdapter);
    }

}
